--
-- Author: shunguo.chen
-- Date: 2019-07-02 11:27:03
--
-- AvatarShopItem 理发店商品item

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

-- data = conf face_res/hair_res/haircolor_res/barber_gender 配置表id
-- shopType = Enums.AvatarShopType
function ClassRef:ctor(data, shopType)
    self.shopType = shopType

    if data then
        self:init(data)
    end
end

function ClassRef:init(id)
    local conf
    if self.shopType == Enums.AvatarShopType.face then
        conf = GD:queryFaceResById(id)
    elseif self.shopType == Enums.AvatarShopType.hair then
        conf = GD:queryHeadResById(id)
    elseif self.shopType == Enums.AvatarShopType.hairColor then
        conf = GD:getHairColorConf(id)
    elseif self.shopType == Enums.AvatarShopType.gender then
        conf = GD:getAvatarGenderConf(id)
    end
    self._conf = conf
    self.id = id

    self._icon = nil
    self._costItem = nil
    self._hairColorItems = nil
    self._isNew = false

    local unlock = tonumber(conf.unlock or 0)
    self._isActive = (unlock == 0)
end

function ClassRef:getId()
    return self.id
end

function ClassRef:getShopType()
    return self.shopType
end

function ClassRef:setIsNew(b)
    self._isNew = b
end
function ClassRef:isNew()
    return self._isNew
end

function ClassRef:setActive(b)
    self._isActive = b
end
function ClassRef:isActive()
    return self._isActive
end

function ClassRef:getName()
    return L(self._conf.name or "")
end

function ClassRef:getIcon()
    if not self._icon then
        self._icon = table.concat({"art/barber_shop/", self._conf.icon, ".png"})
    end
    return self._icon
end

function ClassRef:getCostNum()
    local costItem = self:getCostItem()
    return costItem and costItem:getAmount() or 0
end

function ClassRef:getCostItem()
    if not self._costItem then
        local costItem = UD:parseItemFromString(self._conf.cost)
        self._costItem = costItem
    end
    return self._costItem
end

function ClassRef:getResource()
    local resource = self._conf.resource
    if resource then
        if not self._resource then
            local itemId = tonumber(self._conf.itemID)
            self._resource = GD:getConvertResource(resource, Enums.ItemBagType.item, itemId)
        end
        return self._resource
    end
end

function ClassRef:getHairColorIds()
    if self.shopType == Enums.AvatarShopType.hair then
        local hairColorIDs = table.tonumber(string.split(self._conf.haircolor, ";"))
        return hairColorIDs
    end
end

function ClassRef:isHairColorNew()
    if self._hairColorItems then
        for i,item in ipairs(self._hairColorItems) do
            if item:isNew() then
                return true
            end
        end
    end
    return false
end

function ClassRef:setHairColorItems(items)
    self._hairColorItems = items
end
function ClassRef:getHairColorItems()
    return self._hairColorItems
end

function ClassRef:getHairColorIndex(hairColorID)
    if self._hairColorItems then
        for i,item in ipairs(self._hairColorItems) do
            if item:getId() == hairColorID then
                return i
            end
        end
    end
    return 1
end

function ClassRef:getHairColorID(hairColorIndex)
    if self._hairColorItems then
        local item = self._hairColorItems[hairColorIndex]
        if item then
            return item:getId()
        end
    end
    return 1
end

return ClassRef

